---@class components
---@field modid_tf_whenequip component_modid_tf_whenequip # 通用组件 当装备时 右键切换形态(更换预制物)

---@class component_modid_tf_whenequip
---@field inst ent
---@field tf_to string|nil # 切换后的prefabID
---@field fn_datatf_before (fun(source:ent):table)|nil # 切换前的预制物数据
---@field fn_datatf_after (fun(prod:ent,data:table|nil))|nil # 应用切换后的新预制物数据
---@field keepequip boolean # 切换形态后是否保持装备状态
---@field presets_tf_data {finiteuses:boolean,rechargeable:boolean}|nil
local modid_tf_whenequip = Class(
---@param self component_modid_tf_whenequip
---@param inst ent
function(self, inst)
    self.inst = inst
    -- self.val = 0
    self.tf_to = nil

    self.fn_datatf_before = nil
    self.fn_datatf_after = nil

    self.keepequip = false

    self.presets_tf_data = nil
end,
nil,
{
})

---@param tf_to string # 切换后的prefabID
---@param fn_datatf_before (fun(source:ent):table)|nil # 切换前的预制物数据
---@param fn_datatf_after (fun(prod:ent,data:table|nil))|nil # 应用切换后的新预制物数据
---@param keepequip boolean # 切换后保持装备状态
---@param presets_tf_data {finiteuses:boolean,rechargeable:boolean}|nil # finiteuses rechargeable 等常规组件, 可以用这个, 可以少写点东西, 有待添加
function modid_tf_whenequip:Init(tf_to,fn_datatf_before,fn_datatf_after,keepequip,presets_tf_data)
    self.tf_to = tf_to
    self.fn_datatf_before = fn_datatf_before
    self.fn_datatf_after = fn_datatf_after
    self.keepequip = keepequip
    self.presets_tf_data = presets_tf_data
end

---comment
---@param player ent
function modid_tf_whenequip:DoTF(player)
    assert(self.tf_to ~= nil, 'component:modid_tf_whenequip product not set')

    local skin_name = self.inst:GetSkinName() or nil

    -- 获取源数据
    ---@type table|nil
    local data_source = nil
    if self.fn_datatf_before ~= nil then
        data_source = self.fn_datatf_before(self.inst)
    end

    local presets_tf_data_finiteuse_percent = nil

    local presets_tf_data_rechargeable_total = nil
    local presets_tf_data_rechargeable_percent = nil
    if self.presets_tf_data then
        if self.presets_tf_data.finiteuses then
            presets_tf_data_finiteuse_percent = self.inst.components.finiteuses and self.inst.components.finiteuses:GetPercent() or nil
        end
        if self.presets_tf_data.rechargeable then
            presets_tf_data_rechargeable_total = self.inst.components.rechargeable and self.inst.components.rechargeable.chargetime or nil
            presets_tf_data_rechargeable_percent = self.inst.components.rechargeable and not self.inst.components.rechargeable:IsCharged() and self.inst.components.rechargeable:GetCharge() or nil
        end
    end

    self.inst:Remove()

    -- 懂得都懂
    player:DoTaskInTime(0,function ()
        local new = SpawnPrefab(self.tf_to,skin_name)

        if player.components.inventory then
            player.components.inventory:GiveItem(new)
            if self.keepequip then
                player.components.inventory:Equip(new)
            end
        else
            local pt = player:GetPosition()
            new.Transform:SetPosition(pt:Get())
        end

        -- 传输数据
        if self.fn_datatf_after ~= nil then
            self.fn_datatf_after(new, data_source)
        end

        if presets_tf_data_finiteuse_percent then
            if new.components.finiteuses then
                new.components.finiteuses:SetPercent(presets_tf_data_finiteuse_percent)
            end
        end
        if presets_tf_data_rechargeable_percent and presets_tf_data_rechargeable_total then
            if new.components.rechargeable then
                new.components.rechargeable:Discharge(presets_tf_data_rechargeable_total)
                new.components.rechargeable:SetCharge(presets_tf_data_rechargeable_percent,true)
            end
        end
    end)


    return true
end

return modid_tf_whenequip